import { defineComponent, ref } from 'vue'
import {
  NButton,
  NDataTable,
  NIcon,
  NInput,
  NPagination,
  NSpace,
  NTreeSelect
} from 'naive-ui'
import { SearchOutlined } from '@vicons/antd'
import Card from '@/components/card'
import { useColumns } from './use-columns'
import { useTable } from './use-table'
import CreateUserModal from '../../dialog/create-user-modal'
import { useDepartment } from '@/views/department/tab/department-manage-tab/use-department'
import ModUserDepartmentModal from '../../dialog/mod-user-department-modal'
import ModUserRoleModal from '../../dialog/mod-user-role-modal'
import SetHiveDbSizeModal from '@/views/data-manage/dialog/set-hive-db-size-modal'
import UserDetailModal from '../../dialog/user-detail-modal'

const SysUserManageTab = defineComponent({
  name: 'SysUserManageTab',
  setup() {
    const showCreateUserModalRef = ref(false)
    const { state, updateList, changePage, changePageSize, onOperationClick } =
      useTable()
    const { columnsRef } = useColumns(onOperationClick)
    //部门树下拉选项
    const { departmentVariables, getDepartmentOptions } = useDepartment()
    getDepartmentOptions()

    return {
      state,
      departmentVariables,
      columnsRef,
      onUpdatedList: updateList,
      changePage,
      changePageSize,
      showCreateUserModalRef
    }
  },
  methods: {
    handleCreateUserModalShowChange() {
      this.showCreateUserModalRef = false
    },
    handleModUserDepartmentModalShowChange() {
      this.state.modDepartmemtModalShow = false
    },
    handleModUserRoleModalShowChange() {
      this.state.modRolesModalShow = false
    },
    handleSetHiveDbSizeModalShowChange() {
      this.state.setHiveDbSizeModalShow = false
    },
    handleUserDetailModalShowChange() {
      this.state.userDetailModalShow = false
    },
    onCreateUser() {
      this.showCreateUserModalRef = true
    }
  },
  render() {
    return (
      <>
        <NSpace vertical>
          <Card>
            <NSpace justify='space-between'>
              <NButton onClick={this.onCreateUser} type='info' size='small'>
                {window.$t('user.createUser')}
              </NButton>
              <NSpace>
                <NInput
                  v-model:value={this.state.searchParam.username}
                  clearable
                  size='small'
                  placeholder={window.$t('user.usernameTips')}
                  style='width: 140px'
                />
                <NInput
                  v-model:value={this.state.searchParam.nickname}
                  clearable
                  size='small'
                  placeholder={window.$t('user.nicknameTips')}
                  style='width: 140px'
                />
                <NTreeSelect
                  v-model:value={this.state.searchParam.departmentId}
                  options={this.departmentVariables.departmentOptions}
                  label-field='name'
                  key-field='id'
                  children-field='subs'
                  clearable
                  size='small'
                  placeholder={window.$t('user.departmentTips')}
                  style='width: 180px'
                />
                <NButton type='info' size='small' onClick={this.onUpdatedList}>
                  <NIcon>
                    <SearchOutlined />
                  </NIcon>
                </NButton>
              </NSpace>
            </NSpace>
          </Card>
          <Card>
            <NSpace vertical>
              <NDataTable
                row-class-name='items'
                columns={this.columnsRef.columns}
                size='small'
                data={this.state.list}
                loading={this.state.loading}
                scrollX={this.columnsRef.tableWidth}
              />
              <NSpace justify='end'>
                <NPagination
                  v-model:page={this.state.page}
                  v-model:page-size={this.state.limit}
                  item-count={this.state.count}
                  show-size-picker
                  page-sizes={[10, 30, 50]}
                  show-quick-jumper
                  on-update:page={this.changePage}
                  on-update:page-size={this.changePageSize}
                />
              </NSpace>
            </NSpace>
          </Card>
        </NSpace>
        <CreateUserModal
          showModal={this.showCreateUserModalRef}
          onModalShowChange={this.handleCreateUserModalShowChange}
          onRefreshList={this.onUpdatedList}
        ></CreateUserModal>
        <ModUserRoleModal
          showModal={this.state.modRolesModalShow}
          onModalShowChange={this.handleModUserRoleModalShowChange}
          onRefreshList={this.onUpdatedList}
          userId={this.state.currentRecord.id}
        ></ModUserRoleModal>
        <ModUserDepartmentModal
          showModal={this.state.modDepartmemtModalShow}
          onModalShowChange={this.handleModUserDepartmentModalShowChange}
          onRefreshList={this.onUpdatedList}
          userId={this.state.currentRecord.id}
        ></ModUserDepartmentModal>
        <SetHiveDbSizeModal
          showModal={this.state.setHiveDbSizeModalShow}
          onModalShowChange={this.handleSetHiveDbSizeModalShowChange}
          username={this.state.currentRecord.username}
        ></SetHiveDbSizeModal>
        <UserDetailModal
          showModal={this.state.userDetailModalShow}
          onModalShowChange={this.handleUserDetailModalShowChange}
          userInfo={this.state.currentRecord}
        ></UserDetailModal>
      </>
    )
  }
})

export default SysUserManageTab
